<!--
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-08-19 11:30:52
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-02-23 17:30:39
-->
<template>
  <div class="component_container">
    <!-- 查询表单 -->
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input
            v-model="formInline.address"
            placeholder="请输入接口地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.description"
            placeholder="请输入接口描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.bind_data_item"
            placeholder="请输入返回绑定数据项"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.id"
            placeholder="请输入请求连接ID"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addRequest">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 查询结果列表显示 -->
    <el-table :data="tableData" style="width: 100%" ref="requestLinkTalbe">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="请求连接ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="接口地址">
              <span v-if="!props.row.edit">{{ props.row.address }}</span>
              <el-input v-else v-model="props.row.address"></el-input>
            </el-form-item>
            <el-form-item label="服务器地址">
              <span v-if="!props.row.edit">{{ props.row.service }}</span>
              <el-input v-else v-model="props.row.service"></el-input>
            </el-form-item>
            <el-form-item label="请求触发策略">
              <span v-if="!props.row.edit">{{ props.row.category }}</span>
              <el-input v-else v-model="props.row.category"></el-input>
            </el-form-item>
            <el-form-item label="请求方式">
              <span v-if="!props.row.edit">{{ props.row.mode }}</span>
              <el-select
                v-else
                placeholder="请选择请求方式"
                style="float: left"
                v-model="props.row.type"
              >
                <el-option
                  v-for="item in requestList"
                  :label="item.label"
                  :value="item.code"
                  :key="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="用途描述">
              <span v-if="!props.row.edit">{{ props.row.description }}</span>
              <el-input v-else v-model="props.row.description"></el-input>
            </el-form-item>
            <el-form-item label="最后修改人">
              <span>{{ props.row.last_modified_author }}</span>
            </el-form-item>
            <el-form-item label="最后修改时间" style="width: 100%">
              <span v-if="!props.row.edit">{{
                props.row.last_modified_time
              }}</span>
            </el-form-item>
            <el-form-item label="请求参数" style="width: 100%">
              <span v-if="!props.row.edit">{{ props.row.params }}</span>
              <el-input v-else v-model="props.row.params"></el-input>
              <!-- <el-drag-select
                v-else
                v-model="props.row.page_data_view"
                multiple
                filterable
                placeholder="请选择"
                ref="requestParamsSelect"
                clearable
              >
                <el-option
                  v-for="(item, ind) in requestParamsList"
                  :value="item.code"
                  :key="item.code + ind"
                  :label="item.label"
                ></el-option>
              </el-drag-select> -->
            </el-form-item>
            <el-form-item label="返回绑定数据项" style="width: 100%">
              <span v-if="!props.row.edit">{{ props.row.bind_data_item }}</span>
              <el-input v-else v-model="props.row.bind_data_item"></el-input>
              <!-- <el-drag-select
                v-else
                v-model="props.row.bind_data_item"
                multiple
                filterable
                placeholder="请选择"
                ref="bingDataItemsSelect"
                clearable
              >
                <el-option
                  v-for="(item, ind) in bingDataItemsList"
                  :value="item.code"
                  :key="item.code + ind"
                  :label="item.label"
                ></el-option>
              </el-drag-select> -->
            </el-form-item>
            <el-form-item label="请求编码" style="width: 100%">
              <span v-if="!props.row.edit">{{ props.row.code }}</span>
              <el-input v-else v-model="props.row.code"></el-input>
            </el-form-item>
            <el-form-item label="回调参数" style="width: 100%">
              <span v-if="!props.row.edit">{{
                props.row.callback_params
              }}</span>
              <el-input v-else v-model="props.row.callback_params"></el-input>
            </el-form-item>
            <el-form-item label="排序值" style="width: 100%">
              <span v-if="!props.row.edit">{{ props.row.sort_number }}</span>
              <el-input v-else v-model="props.row.sort_number"></el-input>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="用途描述" prop="description"> </el-table-column>
      <el-table-column label="请求参数" prop="params"> </el-table-column>
      <el-table-column label="返回绑定数据项" prop="bind_data_item">
      </el-table-column>
      <el-table-column label="操作" prop="operate">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="saveEditRequest(scope.row)"
            >保存</el-button
          >
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="cancelEditRequest(scope.row)"
            >取消</el-button
          >
          <el-button
            v-if="!scope.row.edit"
            type="primary"
            @click="editRequest(scope.row)"
            >编辑</el-button
          >
          <el-button type="primary" @click="delRequest(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增请求弹出框 -->
    <el-dialog
      title="新增请求配置"
      :visible.sync="addRequestDialogVisible"
      style="height: 650px"
    >
      <el-form
        :model="addRequestForm"
        label-position="left"
        label-width="120px"
        class="demo-ruleForm"
        status-icon
        :rules="addRules"
        ref="addForm"
        size="medium"
      >
        <el-form-item label="接口请求地址">
          <el-input
            v-model="addRequestForm.address"
            placeholder="请输入接口请求地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="请求方式">
          <el-select
            placeholder="请选择请求方式"
            style="float: left"
            v-model="addRequestForm.type"
          >
            <el-option
              v-for="item in requestList"
              :label="item.label"
              :value="item.code"
              :key="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务器地址">
          <el-input
            v-model="addRequestForm.service"
            placeholder="请输入服务器地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="请求触发策略">
          <el-input
            v-model="addRequestForm.category"
            placeholder="请输入请求触发策略"
          ></el-input>
        </el-form-item>
        <el-form-item label="用途描述">
          <el-input
            v-model="addRequestForm.description"
            placeholder="请输入用途描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="请求参数">
          <el-input
            v-model="addRequestForm.params"
            placeholder="请输入请求参数"
          ></el-input>
        </el-form-item>
        <el-form-item label="返回绑定数据项">
          <el-input
            v-model="addRequestForm.bind_data_item"
            placeholder="请输入返回绑定数据项"
          ></el-input>
        </el-form-item>
        <el-form-item label="回调参数">
          <el-input
            v-model="addRequestForm.callback_params"
            placeholder="请输入回调参数"
          ></el-input>
        </el-form-item>
        <el-form-item label="请求编码">
          <el-input
            v-model="addRequestForm.code"
            placeholder="请输入请求编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序值">
          <el-input
            v-model="addRequestForm.sort_number"
            placeholder="请输入排序值"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRequestDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd('addForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "linkConfig",
  data() {
    return {
      formInline: {
        address: "",
        description: "",
        bind_data_item: "",
        id: "",
      },
      addRequestDialogVisible: false,
      addRules: {},
      addRequestForm: {
        address: "",
        type: "",
        service: "",
        category: "",
        description: "",
        bind_data_item: "",
        params: "",
        callback_params: "",
        sort_number: "",
        code: "",
      },
      requestList: [
        { code: "GET", label: "GET请求" },
        { code: "POST", label: "POST请求" },
        { code: "DELETE", label: "DELETE请求" },
      ],
      tableData: [],
      requestParamsList: [],
      bingDataItemsList: [],
    };
  },
  created() {
    this.getRequestList();
  },
  methods: {
    search() {
      this.getRequestList();
    },
    addRequest() {
      this.addRequestDialogVisible = true;
    },
    confirmAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            create_author: this.$store.state.user.userInfo.login_name || "",
            ...this.addRequestForm,
          };
          this.$http.requestConfig
            .add(this.$address.baseUrl, params)
            .then((res) => {
              if (res.status) {
                this.getRequestList();
                this.addRequestDialogVisible = false;
              }
            })
            .catch((error) => {
              console.log(error);
            });
        }
      });
    },
    getRequestList() {
      this.$http.requestConfig
        .getList(this.$address.baseUrl, { ...this.formInline })
        .then((res) => {
          if (res.status) {
            this.tableData = res.data;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    saveEditRequest(row) {
      let params = {
        ...row,
      };
      this.$delete(params, "edit");
      params.last_modified_author = this.$store.state.user.userInfo.login_name;
      this.$http.requestConfig
        .edit(this.$address.baseUrl, params)
        .then((res) => {
          if (res.status) {
            this.getRequestList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    cancelEditRequest(row) {
      this.$set(row, "edit", false);
      this.$nextTick(() => {
        this.$refs.requestLinkTalbe.toggleRowExpansion(row, false);
      });
    },
    editRequest(row) {
      this.$set(row, "edit", true);
      this.$nextTick(() => {
        this.$refs.requestLinkTalbe.toggleRowExpansion(row, true);
      });
    },
    delRequest(row) {
      this.$http.requestConfig
        .delete(this.$address.baseUrl, { id: row.id })
        .then((res) => {
          if (res.status) {
            this.getRequestList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
<style scoped>
.el-form-item--small.el-form-item {
  float: left;
}
.demo-table-expand div {
  width: 45%;
  margin-left: 40px;
  margin-bottom: 0px;
}
.demo-table-expand div .contentArea {
  width: calc(100% - 200px);
}
</style>